<?php
namespace Home\Controller;
use Think\Controller;
class InterfaceMrController extends Controller {
    public function indexMr(){
        if($_REQUEST['uid']){
            cookie('uid',$_REQUEST['uid']);
        }
        if($_REQUEST['channel']){
            cookie('channel',$_REQUEST['channel']);
        }
        $uid=cookie('uid');
        $channel=cookie('channel');
        $typeName=$_REQUEST['typeName'];

        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if(empty($uid)){
            if (strpos($user_agent, 'MicroMessenger') == false) {
                //非微信浏览器访问
                $uid=date('Ymd').mt_rand(10000,99999).date('His');
                cookie('uid',$uid);
            }else{
                getToken();
                getTicket();
                $wzurl=wxJsdkData();
                if(!empty($_REQUEST['code'])){
                    cookie('code',$_REQUEST['code'],7100);
                }
                getCode($wzurl);
                getOopenid();
                $uid=cookie('openid');
                cookie('uid',$uid);
            }
        }

//        if($typeName=='Qutu'){
//            header("location:https://hy.yixueqm.com/webH5/Qutu2019/#/index?uid={$uid}&channel={$channel}");exit;
//        }
        if(cookie('jploginHC')==''){
            cnzz_pvuv('MR37');//执行统计
            cookie('jploginHC',1,15);
            //名人八字随机数
            csRandNum('mrbzcs',300000,499999);
        }
        $mrbzcs=cookie('mrbzcs');
        header("location:https://hy.yixueqm.com/webH5/Mrtm/#/?uid={$uid}&channel={$channel}&csNumber={$mrbzcs}");
    }

    public function uid(){
        $uid=cookie('uid');
        if(empty($uid)){$uid=date('Ymd').mt_rand(10000,99999).date('His');cookie('uid',$uid);}
        echo cookie('uid');
    }
    public function matching(){//匹配名人数据
        if($_REQUEST['birthday']){//返回操作
//            cookie('znickname',I('post.userna').I('post.userme'));
//            cookie('CSnickname',I('post.userna').'.'.I('post.userme'));
            $userName=I('request.username');
            $birthday=$_REQUEST['birthday'];

            $ymd=mb_substr($birthday,2,10);
            $birthdayArr=explode('-',$birthday);
            $hour=$birthdayArr[4];
            $zsex=$_REQUEST['gender'];

        }else{
            echo '缺少参数';exit;
        }

        zwmp($ymd,$hour,$zsex,'zw');
        $sizhu=I('globals.sizhu');
        $srg=I('globals.srg');//十二宫
        $arrzx=I('globals.arrzx');//主星
        foreach($arrzx as $key=>$value){
            $arrzx[$key]=mb_substr($value,0,2,'utf-8');
            if($key>11){$arrzx[$key]=null;}
        }
        $arrtf=I('globals.arrtf');//天府
        foreach($arrtf as $key=>$value){
            $arrtf[$key]=mb_substr($value,0,2,'utf-8');
            if($key>11){$arrtf[$key]=null;}
        }
        $mgWZ=array_search('命宫',$srg);
        $qyWZ=array_search('迁移',$srg);

        $syzx=I('globals.syzx');//生月诸星 左辅 右弼
        $snzx=I('globals.snzx');//生年支系诸星 天马
        $sxzx=I('globals.sxzx');//时系诸星 地空、地劫
        $sngx=I('globals.sngx');//生年干系诸星 擎羊、陀罗
        $qtx=I('globals.qtx');//其他星 如火星、铃星
        $mrbzArr=mingrenbaziZW($sizhu,$mgWZ,$arrzx,$arrtf,$syzx,$snzx,$sxzx,$sngx,$qtx,$qyWZ);//名人八字
        $gwData=array(
            $arrzx[$mgWZ], $arrtf[$mgWZ], $syzx[$mgWZ], $snzx[$mgWZ], $sxzx[$mgWZ], $sngx[$mgWZ], $qtx[$mgWZ],
        );
        if($arrzx[$mgWZ].$arrtf[$mgWZ]){
        }else{//命宫没有主星
            $gwData=array('(迁宫)',$arrzx[$qyWZ],$arrtf[$qyWZ],$syzx[$qyWZ],$snzx[$qyWZ],$sxzx[$qyWZ],$sngx[$qyWZ],$qtx[$qyWZ],);
        }

        $gwData=array_filter($gwData);
        foreach($gwData as $key=>$value){
            if($key>0){
                $gwData[$key]=mb_substr($value,0,2,'utf-8');
                if(mb_strlen($value,'utf-8')>4){
                    array_push($gwData,mb_substr($value,3,2,'utf-8'));
                }
            }
        }

        $mrbzArr['userData']=array(
            'username'=>$userName,
            'bazi'=>$sizhu,
            'ziwei'=>$gwData,
        );
        //dump($mrbzArr);
        echo json_encode($mrbzArr);
    }
    public function characterZW(){//紫微性格
        if($_REQUEST['birthday']){//返回操作
            $userName=I('request.username');
            $birthday=$_REQUEST['birthday'];
            $ymd=mb_substr($birthday,2,10);
            $birthdayArr=explode('-',$birthday);
            $hour=$birthdayArr[4];
            $zsex=$_REQUEST['gender'];

        }else{
            echo '缺少参数';exit;
        }

        zwmp($ymd,$hour,$zsex,'zw');
        zsqk(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'));
        $zsqkzx=I('globals.zsqkzx');//自身主星
        $mgarrzhux=I('globals.mgarrzhux');//自身主星状况

        $Arr=array(
            'username'=>$userName,
            'zhuxing'=>$zsqkzx.'入命宫',
            'youdian'=>$mgarrzhux[0]['youdian'],
            'quedian'=>$mgarrzhux[0]['quedian'],
            'minggong'=>$zsqkzx.'坐命宫',
            'text'=>$mgarrzhux[0]['text'],
        );
        //dump($Arr);
        echo json_encode($Arr);exit;
    }
    public function jieguoye(){//获取结果内容
        $orderid=$_REQUEST['orderid'];
        judgeData(array($orderid));

        $arrdata=M()->query("select ordernum,status,username,sex,year,month,day,hour,minute from sm_h5test where ordernum='{$orderid}' limit 1");
        $ymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
        $hour=$arrdata[0]['hour'];
        $zsex=$arrdata[0]['sex'];


        $m=mb_substr($ymd,5,2); //月
        $d=mb_substr($ymd,8,2);//日

        $znickname=$arrdata[0]['username'];
        $CSnickname=mb_substr($znickname,0,1,'utf-8').'.'.mb_substr($znickname,1,3,'utf-8');

        zwmp($ymd,$hour,$zsex,'zw');
        $sizhu=I('globals.sizhu');
        $shengxiao=I('globals.shengxiao');//生肖
        zsqk(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'));
        $zsqkzx=I('globals.zsqkzx');//自身主星
        $mgarrzhux=I('globals.mgarrzhux');//自身主星状况

        $ssarray=array();
        array_push($ssarray,implode(I('globals.nianzhuss')));array_push($ssarray,implode(I('globals.yuezhuss')));
        array_push($ssarray,'命主');array_push($ssarray,implode(I('globals.shizhuss')));

        $y=mb_substr($ymd,0,4);//年
        $m=mb_substr($ymd,5,2); //月
        $d=mb_substr($ymd,8,2);//日
        $zyinli=yinli($y,$m,$d);
        $zyangli=$y.'年'.$m.'月'.$d.'日'.' '.$hour.'时';//阳历
        $zyinli=$zyinli[0].'年'.$zyinli[1].$zyinli[2]." ".mb_substr($sizhu[3],1,1,'utf-8')."时";//阴历

        $Arr['xingge']=array(
            'zhuxing'=>$zsqkzx.'入命宫',
            'youdian'=>$mgarrzhux[0]['youdian'],
            'quedian'=>$mgarrzhux[0]['quedian'],
            'minggong'=>$zsqkzx.'坐命宫',
            'text'=>$mgarrzhux[0]['text'],
        );

        $dayunNum=I('globals.dayun');
        $selectArr=array(
            'nickname'=>$znickname,
            'sex'=>$zsex,
            'shengxiao'=>$shengxiao,
            'yangli'=>$zyangli,
            'yinli'=>$zyinli,
            'sizhu'=>I('globals.sizhu'),
            'nianzhuss'=>$ssarray,
            'canggan'=>I('globals.canggan'), 'zhishen'=>I('globals.zhishen'), 'sizhuny'=>I('globals.sizhuny'), 'bzdishi'=>I('globals.bzdishi'),
            'wxsqs'=>I('globals.wxsqs'), 'xiys'=>I('globals.xiys'), 'taiyuan'=>I('globals.taiyuan'), 'rikong'=>I('globals.rikong'),
            'dayun'=>array($dayunNum+1,$dayunNum+11,$dayunNum+21,$dayunNum+31,$dayunNum+41,$dayunNum+51,$dayunNum+61,$dayunNum+71),//大运
            'dayungz'=>I('globals.dayungz'),//大运干支
            'dayunss'=>I('globals.dayunss'),//大运十神
        );
        $Arr['wuxing']=$selectArr;

        $nameArr=explode('.',$CSnickname);
        $xing=$nameArr[0];
        $ming=$nameArr[1];
        $Trans = new \Library\Trans();
        $fxing=$Trans->c2t($xing);//转繁体
        $fming=$Trans->c2t($ming);
        $fnickname=$fxing.$fming;
        xingmingZft($fxing,$fming);//获取笔画
        $bihua=I('globals.bihua');//笔画
        xmcs($ymd,$hour);
        $trdwz=I('globals.trdwz');//天人地外总
        $hudongGX=I('globals.hudongGX');//互动关系

        $i=0;$trdwzArr=array();
        foreach($trdwz as $key=>$value){
            $titleData=array('天格','人格','地格','外格','总格');
            $trdwzArr[$i]=$value;
            $trdwzArr[$i]['titme']=$titleData[$i];
            $i++;
        }

        foreach($hudongGX as $key=>$value){
            $nameText=array('从五格互动看基础运','从五格互动看成功运','从五格互动看社交运','从五格互动看家庭社交运','从五格互动看个人特性','从五格互动看家庭关系');
            $hudongGX[$key]['title']=$nameText[$key];
        }
        $Arr['wuge']=array(
            'nickname'=>$znickname,
            'fnickname'=>$fnickname,
            'bihua'=>$bihua,
            'trdwz'=>$trdwzArr,
            'hudongGX'=>$hudongGX,
        );

        $zhanxing=new \Library\ZhanXing();
        $constellation=$zhanxing->constellation($m,$d);//获取星座
        $lnyuncheng2019=lnyuncheng2019($ymd,I('globals.dayun'),I('globals.dayunss'),$constellation,$hour);
        $Arr['yc2019']=array(
            'yunshi'=>$lnyuncheng2019['yunshi'],
            'monthArr'=>$lnyuncheng2019['monthArr']
        );
        //dump($Arr);

        echo json_encode($Arr);exit;
    }
    public function shareData(){//微信分享参数获取
        $url=$_REQUEST['shareUrl'];
//        file_put_contents('./Upload/shareData.txt',$_REQUEST);
//        file_put_contents('./Upload/shareDataUrl.txt',$url);
        judgeData(array($url));
        //$url='https://hy.yixueqm.com/webH5/Mrtm/';
        getToken();
        getTicket();
        $id = cookie('APPID');

        $noncestr=noncestr(15);
        $timestamp=time();
        $ticket= S('ticket'.$id);
        $wzurl=$url;
        $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

        $Arr=array(
            //'request'=>$_REQUEST,
            'data'=>"jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}",
            'url'=>$url,
            'appid'=>cookie('APPID'),
            'timestamp'=>"{$timestamp}",
            'noncestr'=>$noncestr,
            'signature'=>$signature
        );
        echo json_encode($Arr);
    }

    public function placeOrder(){//下单支付
        $uid=$_REQUEST['uid'];
        $channel=$_REQUEST['channel'];
        $userName=I('request.username');
        $birthday=$_REQUEST['birthday'];
        $ymd=mb_substr($birthday,2,10);
        $birthdayArr=explode('-',$birthday);
        $hour=$birthdayArr[4];
        $zsex=$_REQUEST['gender'];
        judgeData(array($userName,$birthday,$uid));

        $orderid='MR37'.date('Ymd').mt_rand(1000,9999).date('His');
        cookie('orderid',$orderid);

        //创建订单

        $y=mb_substr($ymd,0,4);//年
        $m=mb_substr($ymd,5,2); //月
        $d=mb_substr($ymd,8,2);//日
        $datetype=mb_substr($birthday,0,1);
        cookie('price',58);//默认价格
        csPay($userName,$channel);//测试价格判断

        $price=cookie('price');
        $arrdata=M()->query("select id from sm_h5test where ordernum='{$orderid}' limit 1");
        if(empty($arrdata)){
            M()->query("insert into sm_h5test (ordernum,price,username,typeid,sex,status,datetype,year,month,day,hour,paykind,appuserid,ip,channel)values(
                                          '{$orderid}','{$price}','{$userName}',37,'{$zsex}',-1,'{$datetype}',{$y},{$m},{$d},{$hour},-1,'{$uid}','{$_SERVER['REMOTE_ADDR']}','{$channel}')");
        }else{
            echo '订单有误';exit;
        }
        redirect(U('interfaceMr/zhifu','',false)."?orderid={$orderid}");
    }
    public function zhifu(){
        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {//微信公众号支付
            //非微信浏览器访问
        }else{
            getToken();
            getTicket();
            $wzurl = wxJsdkData();
            if (!empty($_REQUEST['code'])) {
                cookie('code', $_REQUEST['code'], 7100);
            }
            getCode($wzurl);
            getOopenid();
        }

        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问

            //支付宝
            $this->assign('ordername1','名人同命');
            $this->assign('ordername2','知命-名人同命');
            $this->display("Index/pay");

            $this->assign('wxLogin',0);
        }else{
            $this->assign('wxLogin',1);
            $xml=wxgzhzf('MR37','名人同命',cookie('orderid'));//公众号支付回调参数
            $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $val = json_decode(json_encode($xmlstring),true);
            $noncestr=noncestr(15);//随机字符串
            $timestamp=time();//时间戳

            $stringA="appId=".cookie('APPID')."&nonceStr={$noncestr}&package=prepay_id={$val['prepay_id']}&signType=MD5&timeStamp={$timestamp}";
            $stringSignTemp=$stringA."&key=".cookie('wxKEY');
            $sign=strtoupper(md5($stringSignTemp));

            $this->assign('timeStamp',$timestamp);
            $this->assign('nonceStr',$noncestr);
            $this->assign('package','prepay_id='.$val['prepay_id']);
            $this->assign('signType','MD5');
            $this->assign('paySign',$sign);
            $this->assign('appId',cookie('APPID'));
            $orderid=cookie('orderid');
            $this->assign('jumpUrl',"https://hy.yixueqm.com/webH5/Mrtm/#/mrResultEnd?orderid={$orderid}");
            $this->assign('returnUrl','https://hy.yixueqm.com/webH5/Mrtm/#/mrResultPay');
            $this->display("Index/paywx");
        }
    }
}